# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import openpyxl
# useful for handling different item types with a single interface
from itemadapter import ItemAdapter


class MyspiderPipeline:

    def __init__(self):
        # 生成excel表
        self.workbook=openpyxl.Workbook()
        self.sheet=self.workbook.active
        self.sheet.append(["书名","介绍","热评"])


    def process_item(self, item, spider):
        # 将图书信息添加到excel表
        self.sheet.append([item["name"],item["info"],item['quote']])
        return item

    def close_spider(self,spider):
        self.workbook.save("book.xlsx")
        self.workbook.close()